{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-09T03:11:13.651915Z",
     "start_time": "2025-04-09T03:11:13.647813Z"
    }
   },
   "source": [
    "#列表转ndarray\n",
    "import numpy as np\n",
    "list1 = [[1,2],[3,4],[5,6]]\n",
    "print(list1)\n",
    "print(type(list1))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2], [3, 4], [5, 6]]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:11:15.106988Z",
     "start_time": "2025-04-09T03:11:15.101700Z"
    }
   },
   "cell_type": "code",
   "source": [
    "array = np.array(list1)\n",
    "print(type(array))\n",
    "print(array)"
   ],
   "id": "e6864d02d738ab8a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:12:06.113742Z",
     "start_time": "2025-04-09T03:12:06.109777Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(array.shape)\n",
    "print(array.ndim)\n",
    "print(array.size)\n",
    "print(array.dtype)"
   ],
   "id": "960f777822faffe8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 2)\n",
      "2\n",
      "6\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:11:16.657581Z",
     "start_time": "2025-04-09T03:11:16.652856Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#ndarray转列表,不能在原有基础上转，而是生成了一个新的列表\n",
    "array1 = array.tolist()\n",
    "print(type(array1))\n",
    "print(array1)"
   ],
   "id": "c8a0c490059ae73a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "[[1, 2], [3, 4], [5, 6]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:19:00.305578Z",
     "start_time": "2025-04-09T03:19:00.300776Z"
    }
   },
   "cell_type": "code",
   "source": [
    "array2 = array.reshape((6,),order='C')\n",
    "array3 = array.flatten()\n",
    "print(array2)\n",
    "print(array3)"
   ],
   "id": "15473e65d48af819",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:24:12.799054Z",
     "start_time": "2025-04-09T03:24:12.795052Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#改变维度\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(f'shape{t.shape}')\n",
    "print(t.ndim)\n",
    "t1= t.reshape(4,6)\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "id": "23a52ff893543b61",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "shape(24,)\n",
      "1\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:25:43.688921Z",
     "start_time": "2025-04-09T03:25:43.684635Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t2 = t.reshape(2,3,4)\n",
    "print(t2)\n",
    "print(t2.shape)\n",
    "print(t2.ndim)"
   ],
   "id": "663775b8bfdfe3d6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "3\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-09T03:27:17.737265Z",
     "start_time": "2025-04-09T03:27:17.733149Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t3 = t.reshape(2,3,2,2)\n",
    "print(t3)\n",
    "print(t3.shape)\n",
    "print(t3.ndim)"
   ],
   "id": "94cef6922e9e81ea",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[ 0  1]\n",
      "   [ 2  3]]\n",
      "\n",
      "  [[ 4  5]\n",
      "   [ 6  7]]\n",
      "\n",
      "  [[ 8  9]\n",
      "   [10 11]]]\n",
      "\n",
      "\n",
      " [[[12 13]\n",
      "   [14 15]]\n",
      "\n",
      "  [[16 17]\n",
      "   [18 19]]\n",
      "\n",
      "  [[20 21]\n",
      "   [22 23]]]]\n",
      "(2, 3, 2, 2)\n",
      "4\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "18f2c9c8d7bf277b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
